import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/common/Config.dart';
import 'package:my_flutter_github/common/LocalStorage.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';

import 'package:my_flutter_github/redux/MyState.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/FlexButton.dart';
import 'package:my_flutter_github/widgets/InputWidget.dart';
import 'package:flutter_redux/flutter_redux.dart';

//登录界面
class LoginPage extends StatefulWidget {
  static const sName = "/login";

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  var _userName = "";
  var _password = "";

  final TextEditingController _userNameController = new TextEditingController();
  final TextEditingController _pwController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    initParams();
  }

  void initParams() async {
    _userName = await LocalStorage.get(Config.USER_NAME_KEY);
    _password = await LocalStorage.get(Config.PW_KEY);
    _userNameController.value = new TextEditingValue(text: _userName ?? "");
    _pwController.value = new TextEditingValue(text: _password ?? "");
  }

  @override
  Widget build(BuildContext context) {
    return new StoreBuilder<MyState>(builder: (context, store) {
      return new GestureDetector(
        //加了这个才能在点击空白区域 触摸隐藏键盘
        behavior: HitTestBehavior.translucent,
        onTap: () {
          //触摸隐藏键盘
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: new Container(
          color: Theme.of(context).primaryColor,
          child: new Center(
            child: new Card(
              elevation: 5.0,
              shape: new RoundedRectangleBorder(borderRadius: BorderRadius.all(new Radius.circular(10.0))),
              color: Colors.white,
              margin: EdgeInsets.all(30.0),
              child: new Padding(
                  padding: EdgeInsets.only(left: 30.0, top: 30.0, right: 30.0, bottom: 60.0),
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      new Image(image: new AssetImage(MyIcons.DEFAULT_USER_ICON), width: 90.0, height: 90.0,),
                      new Padding(padding: EdgeInsets.all(10.0)),
                      new InputWidget(
                        hintText: "请输入Github用户名",
                        iconData: MyIcons.LOGIN_USER,
                        obscureText: false,
                        onChanged: (String value) {
                          _userName = value;
                        },
                        controller: _userNameController,
                      ),
                      new Padding(padding: EdgeInsets.all(10.0)),
                      new InputWidget(
                        hintText: "请输入密码",
                        iconData: MyIcons.LOGIN_PW,
                        obscureText: true,
                        onChanged: (String value) {
                          _password = value;
                        },
                        controller: _pwController,
                      ),
                      new Padding(padding: new EdgeInsets.all(30.0)),
                      new FlexButton(
                        text: "登录",
                        color: Theme.of(context).primaryColor,
                        textColor: Colors.white,
                        onPress: (){
                          if(_userName == null || _userName.isEmpty) {
                            return;
                          }
                          if(_password == null || _password.isEmpty) {
                            return;
                          }

                          CommonUtils.showLoadingDialog(context);
                          UserDao.login(_userName, _password, store).then((result) {
                                Navigator.pop(context);
                                if(result != null && result.result) {
                                  Future.delayed(const Duration(seconds: 1), (){
                                      NavigatorUtils.goHome(context);
                                      return true;
                                  });
                                }
                          });
                        },
                      )
                    ],
                  ),
              ),
            ),
          ),
        ),
      );
    });
  }
}
